#ifndef HASHDB_H
#define HASHDB_H
/**
 * @file hdb.h
 * @brief hash db, like dbm
 */

#include <stdint.h>

#ifdef __cplusplus
extern "C" {
#endif

#define HDB_MALLOC 1
//#define HDB_

typedef enum hdb_action_e
{
    HDB_CONTINUE = 1 << 0,
    HDB_BREAK    = 1 << 1,
    HDB_REMOVE   = 1 << 2
}hdb_action_e;

typedef struct hdatum hdatum_t;
struct hdatum
{
    void* data;
    int16_t size;
    int16_t flags;
};

typedef struct hdb hdb_t;
typedef uint32_t (*hdb_hashfunc_t)(hdatum_t* datum);
typedef hdb_action_e (* hdb_iterfunc_t)(void* ctx, hdatum_t* key, hdatum_t* val);

hdb_t* hdb_create(int32_t bnum, void* mem_base, int32_t mem_size);
hdb_t* hdb_open(void* mem_base);
void hdb_close(hdb_t* me);
void hdb_enable_resize(hdb_t* me, int (*resize)(void* ctx, uint32_t newsize), void* ctx);

int hdb_add(hdb_t* me, hdatum_t* key, hdatum_t* val);
int hdb_set(hdb_t* me, hdatum_t* key, hdatum_t* val);
int hdb_get(hdb_t* me, hdatum_t* key, hdatum_t* val);
int hdb_rm(hdb_t* me, hdatum_t* key, hdatum_t* val);

void hdb_iterate(hdb_t* me, hdb_iterfunc_t iter, void* ctx);

void hdb_set_hash(hdb_t* me, hdb_hashfunc_t func);

#ifdef __cplusplus
}
#endif
#endif /* HASHDB_H */
